Problem 2: get today's facebook posting for network
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.5
Graph-API: get data into and out of Facebooks's social graph.
https://developers.facebook.com/docs/graph-api/overview
Essentially, the api is a http get url to returns the graph in the content of the page.
Example call the pictures from your graph http://graph.facebook.com/facebook/picture?redirect=false
For python we can use the urllib to simplify retrieval https://docs.python.org/2.7/howto/urllib2.html
In [2]:
import urllib2
response = urllib2.urlopen('http://graph.facebook.com/facebook/picture?redirect=false')
html = response.read()
print html
To make this easy you should register as a Facebook Developer at https://developers.facebook.com/apps
Login to you Facebook and acount and register as a developer. You will need to create a app name and setup you security details. Create an App ID, API Version, and AppSecret Store them in file outside your p
In [3]:
import urllib2
appSecret="7e141bf4406afafeaa43579d1f2d7fd8"
appId="547808358705887"
requesturl = 'http://graph.facebook.com/oauth/access_token?client_id='+appId+'&client_secret='+appSecret+'&grant_type=client_credentials'
print requesturl
try:
response = urllib2.urlopen(requesturl)
html = response.read()
print html
except IOError, e:
print 'Failed to open "%s".' % requesturl
if hasattr(e, 'code'):
print 'We failed with error code - %s.' % e.code
elif hasattr(e, 'reason'):
print "The error object has the following 'reason' attribute :"
print e.reason
print "This usually means the server doesn't exist,",
print "is down, or we don't have an internet connection."
However we need access tokens to really use the service. Unlike javascript with is embedded into a browser. Python scripts will be standalone programs that cannot normally share the access in a browser. So let us pull down and use a python sdk to simplify some of the setup. Retrieve and install: https://github.com/pythonforfacebook/facebook-sdk
Use this interface to get a temporary access token https://developers.facebook.com/tools/explorer/
In [21]:
#Copied from https://github.com/pythonforfacebook/facebook-sdk/blob/master/examples/get_posts.py
import facebook
import requests
# You'll need an access token here to do anything. You can get a temporary one
# here: https://developers.facebook.com/tools/explorer/
access_token = 'CAACEdEose0cBANcRP7PXpdeDZBZAZAZBNK0UuZAcAvEHAZB8ZCbyyEWFOlTDeZCb6mwGQCffdbx55Pl3EarMHZBYmH0wcPZBnjYms7qcZA6PNPPp6ITp6TSjtfFMWCNKXAIq3DLobYpj7rC5kV1WrKyasqvkvZBrFEhZCFEPvxheb7YZBR00gjpZCpzwNZA3kMdk7BbhoeK4OAN1ZCBBuleQfnynyWLLgzJDCZBuXqWD0ZD'
# Look at python site.
user = 'python'
def some_action(post):
""" Here you might want to do something with each post. E.g. grab the
post's message (post['message']) or the post's picture (post['picture']).
In this implementation we just print the post's created time.
"""
print(post['created_time'] + "==================================================")
print(post['message'])
print("=========================================================================")
graph = facebook.GraphAPI(access_token)
#profile = graph.get_object(user)
#print profile
posts = graph.get_object(fields='posts')
while True:
try:
# Perform some action on each post in the collection we receive from
# Facebook.
[some_action(post=post) for post in posts['message']]
# Attempt to make a request to the next page of data, if it exists.
posts = requests.get(posts['paging']['next']).json()
except KeyError:
# When there are no more pages (['paging']['next']), break from the
# loop and end the script.
break
In [17]:
import requests
appSecret="7e141bf4406afafeaa43579d1f2d7fd8"
appId="547808358705887"
r = requests.get('https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='+appId+'&client_secret='+appSecret)
access_token = r.text.split('=')[0]
print access_token
In [ ]: